package com.supermali.behavior.down.land;

import com.supermali.behavior.Gravity;
import com.supermali.behavior.down.DownBehavior;
import com.supermali.entity.npc.IntesectDirect;
import com.supermali.entity.npc.person.Person;

import java.util.Set;

public class LandPersonDown extends DownBehavior {

    private boolean isOver;

    private Person person;
    // 重力: 控制下落速度
    private Gravity gravity;
    // 重力下落总时间
    private double totalTime;
    // 下降的初始速度
    private double v0 ;

    public LandPersonDown(Person person) {
        this.person = person;
        gravity = new Gravity(person);
        totalTime = 0;
        isOver =true;
        v0 = 0;
    }

    /**
     * 下落未结束，始终保持下落状态
     * */
    @Override
    public void down() {
        person.setRunState(Person.RunState.DOWN);
        isOver= false;
        totalTime+=Gravity.t;
        double y = this.person.getY();
        double dy = v0*Gravity.t-0.5*Gravity.g*Gravity.t*totalTime;
        y=y+dy;
        person.setY(y);
        // 检测是否具有碰撞
        Set<IntesectDirect> intesectDirects = person.checkCollide();
        for(IntesectDirect direct: intesectDirects){
            if(direct.getDirect()== IntesectDirect.Direct.TOP){
                isOver = true;
                totalTime = 0;
            }
        }
    }

    public void setV0(double v0) {
        this.v0 = v0;
    }

    @Override
    public boolean isOver() {
        return isOver;
    }
}
